import requests
import base64
import json
class MoCloud ():
    url = "http://ocr.xinby.cn/api.php"
    def __init__(self, username, password):
        self.Http = requests.session()
        self.username = username
        self.password = password
    def login(self):
        Data = {
            "user": self.username,
            "pass": self.password,
            "act": "login"
        }
        request = self.Http.post(MoCloud.url, Data)
        JSON = request.json()
        if JSON["code"] == 1:
            JSON = JSON["data"]
            self.id = JSON["id"]
            self.token = JSON["token"]
            self.type = JSON["type"]
            self.bp = JSON["bp"]
            return True
        else:
            self.errmsg ="[%d]%s" % (JSON["code"], JSON["errmsg"])
            return False
    def GET_errmsg(self):
        return self.errmsg
    def GET_id(self):
        return self.id
    def GET_token(self):
        return self.token
    def GET_type(self):
        return self.type
    def GET_bp(self):
        return self.bp
    def image(self, typeid, image, dev_id):
        Data = {
            "user": self.username,
            "pass": self.password,
            "act": "image",
            "type": "shibie",
            "typeid": typeid,
            "image": base64.b64encode(image),
            "dev_id": dev_id,
        }
        request = self.Http.post(MoCloud.url, Data)
        JSON = request.json()
        if JSON["code"] == 1 :
            JSON = JSON["data"]
            self.hash = JSON["hash"]
            self.result = JSON["result"]
            return True
        else:
            self.errmsg = "[%d]%s" % (JSON["code"], JSON["errmsg"])
            return False
    def GET_result(self):
        return self.result
    def baocuo(self):
        Data = {
            "user": self.username,
            "pass": self.password,
            "act": "image",
            "type": "baocuo",
            "hash": self.hash,
        }
        request = self.Http.post(MoCloud.url, Data)
        JSON = request.json()
        if JSON["code"] == 1:
            return True
        else:return False

ocr = MoCloud("18888888888", "123456") #前往魔云开放平台注册账号
data = ocr.login()
if data == False:
    print(ocr.GET_errmsg())
else :
    print(ocr.GET_bp())
    request = requests.get("http://edu.henanjf.com/Login/VerifyCode")
    print(ocr.image(3040, request.content, "dev_id"), ocr.GET_result()) #dev_id 开发者ID

